package com.mage.servlet;

import com.mage.bean.Chongzhi;
import com.mage.bean.User;
import com.mage.dao.ChongzhiDao;
import com.mage.dao.UserDao;
import com.mage.dao.impl.ChongzhiDaoImpl;
import com.mage.dao.impl.UserDaoImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

@WebServlet("/VipChongzhiServlet")
public class VipChongzhiServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;
    public VipChongzhiServlet() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        try {
            //1. 获取客户端提交上来的数据
            int userid = Integer.parseInt(request.getParameter("userid"));
            String username = request.getParameter("username");
            Double money = Double.parseDouble(request.getParameter("money"));
            int usertype = Integer.parseInt(request.getParameter("usertype"));
            String czmethod = request.getParameter("czmethod");
            Date createtime = new Date();
            System.out.println(username);
            //2. 添加到数据库
            Chongzhi chongzhi = new Chongzhi(0,userid,username,money,czmethod,createtime);
            ChongzhiDao service = new ChongzhiDaoImpl();
            service.insert(chongzhi);
            UserDao userService = new UserDaoImpl();
            User user = userService.findUserById(userid);
            user.setMoney(user.getMoney()+money);
            user.setWxf(user.getWxf()+money);
            userService.update(user);
            if (usertype == -1){
                request.getRequestDispatcher("ChongzhiListServlet").forward(request, response);
            }else {
                request.getRequestDispatcher("UserInfoServlet").forward(request, response);
            }


        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
